// 用户信息相关的store，后续相关数据和逻辑都放在这里，已有的数据和逻辑也可以迁移到这里
import { DEFAULT_USER_INFO } from '@/assets/constant/default-constant';
import { observable, action } from 'mobx-miniprogram';
import { getUserInfo } from '@/api/user';

export const userStore = observable({
  /** 用户基本信息 */
  userInfo: JSON.parse(
    JSON.stringify(DEFAULT_USER_INFO),
  ) as typeof DEFAULT_USER_INFO,

  /** 获取并设置用户信息 */
  getUserInfo: action(async function () {
    const res = await getUserInfo(wx.getStorageSync('token'));
    console.log('getUserInfo', res);
    this.userInfo = {
      id: res.id || DEFAULT_USER_INFO.id,
      avatar: res.avator || DEFAULT_USER_INFO.avatar,
      nickname: res.emcee_name || DEFAULT_USER_INFO.nickname,
      isShowNewMembership:
        res?.isShowNewMembership || DEFAULT_USER_INFO.isShowNewMembership,
    };
    console.log('this.userInfo', this.userInfo);
  }),
});
